package com.xindaoapp.happypet.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.application.HappyPetApplication;
import com.xindaoapp.happypet.bean.MessageCenterInfo;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.IRequest;
import com.xindaoapp.happypet.view.CircleImageView;

public class AtMessageCenterAdapter extends XinDaoBaseAdapter<MessageCenterInfo> {

    public enum MESSAGETYPE {
        /** 私信 */
        APP_PM("app_pm"),
        /** 回复 */
        POST("post"),
        /** 系统消息 */
        SYSTEM("system"),
        /** at 我 */
        AT("at");

        public String type;

        private MESSAGETYPE(String type) {
            this.type = type;
        }

    }

    /**
     * 私信类型
     * 
     * @author chenghao
     * 
     */
    enum APPPMTYPE {
        /** 我对谁说 */
        FA("fa"),
        /** 谁对我说 */
        SHOU("shou");
        String type;

        private APPPMTYPE(String type) {
            this.type = type;
        }
    }

    /**
     * 回复类型
     * 
     * @author chenghao
     * 
     */
    enum REPLYTYPE {
        /** 回复楼主 */
        POST("post"),
        /** 回复楼层 */
        QUOTE("quote");
        String type;

        private REPLYTYPE(String type) {
            this.type = type;
        }
    }

    public AtMessageCenterAdapter(Context context, List<MessageCenterInfo> iniData, int pageSize,
            int res, int loadingRes) {
        super(context, iniData, pageSize, res, loadingRes);
    }

    @Override
    public void nextPage(int start, int size,
            final ILoadNextPageData<MessageCenterInfo> iLoadNextPageData) {
        super.nextPage(start, size, iLoadNextPageData);
        HappyPetApplication.get().getMoccaApi()
                .getAtMessageCenterInfos(start, size, new IRequest<List<MessageCenterInfo>>() {

                    @Override
                    public void request(List<MessageCenterInfo> t) {
                        iLoadNextPageData.loadNextPageData(t == null ? null : t);
                    }
                });
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent,
            final MessageCenterInfo value) {
        ViewHolder holder = (ViewHolder) convertView.getTag();
        if (holder == null) {
            holder = new ViewHolder();
            holder.icon = (CircleImageView) convertView.findViewById(R.id.user_icon);
            holder.title = (TextView) convertView.findViewById(R.id.title);
            holder.time = (TextView) convertView.findViewById(R.id.time);
            holder.content = (TextView) convertView.findViewById(R.id.content);
            holder.count = (TextView) convertView.findViewById(R.id.count);
            holder.isNew = (ImageView) convertView.findViewById(R.id.red_point);
            convertView.setTag(holder);
        }
        ImageLoader.getInstance().displayImage(value.userface, holder.icon);
        holder.title.setText(value.author);
        holder.content.setText(CommonUtil.convetToHtml(
                CommonUtil.parseEmoji(String.format("在 %s 主题帖中@了你!", value.subject)), mContext));
        holder.count.setVisibility(View.VISIBLE);


        if (value.isnew == 1) {
            holder.count.setVisibility(View.VISIBLE);
            holder.isNew.setVisibility(View.VISIBLE);
            holder.count.setText(String.valueOf(value.from_num > 99 ? "99+" : value.from_num));
        } else {
            holder.count.setVisibility(View.GONE);
            holder.isNew.setVisibility(View.GONE);
        }

        holder.time.setText(CommonUtil.formatTime(value.addtime));


        return convertView;
    }


    public class ViewHolder {
        public CircleImageView icon;
        public TextView title;
        public TextView content;
        public TextView time;
        public ImageView isNew;
        public TextView count;
        public TextView replyContent;
        public View privateMessageLogo;
    }

}
